package cn.edu.scau.cmi.liyunlin.hibernate.contorller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import cn.edu.scau.cmi.liyunlin.hibernate.dao.StudentDao;
import cn.edu.scau.cmi.liyunlin.hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.liyunlin.hibernate.domain.Student;
import cn.edu.scau.cmi.liyunlin.hibernate.domain.Teacher;
import cn.edu.scau.cmi.liyunlin.spring.util.ApplicationContextUtil;
import javafx.event.ActionEvent;
import javafx.scene.control.TextArea;

public class HibernateSpringMixController {
	@FXML
	private TextField stuAddId;
	@FXML
	private TextField stuAddName;
	@FXML
	private Button stuAddBtn;
	@FXML
	private TextField stuDelId;
	@FXML
	private Button stuDelBtn;
	@FXML
	private Button stuUpdateBtn;
	@FXML
	private TextField stuUpdName;
	@FXML
	private TextField stuUpdId;
	@FXML
	private TextField teaUpdId;
	@FXML
	private TextField teaUpdName;
	@FXML
	private Button teaUpdBtn;
	@FXML
	private Button teaDelBtn;
	@FXML
	private TextField teaDelId;
	@FXML
	private Button teaAddBtn;
	@FXML
	private TextField teaAddName;
	@FXML
	private TextField teaAddId;
	@FXML
	private Button stuChooseBtn;
	@FXML
	private TextField stuChooseTid;
	@FXML
	private TextField stuChooseSid;
	@FXML
	private TextField stuUpdSid;
	@FXML
	private TextField stuUpdTid;
	@FXML
	private Button stuUpdBtn;
	@FXML
	private Button TeaUpdBtn;
	@FXML
	private TextField TeaUpdSid;
	@FXML
	private TextField TeaUpdTid;
	@FXML
	private TextField TeaChooseTid;
	@FXML
	private TextField TeaChooseSid;
	@FXML
	private Button TeaChooseBtn;
	@FXML
	private TextArea msgBox;
	
	/**
	 * 新增学生
	 * @param event
	 */
	@FXML
	public void addStudent(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		Transaction transaction = studentDao.getSession().beginTransaction();
		Student student=new Student();
		student.setName(stuAddName.getText());
		student.setId(Long.parseLong(stuAddId.getText()));
		studentDao.save(student);
		msgBox.setText(stuAddName.getText() + "学生添加成功:" + student.toString() + "\n");
		transaction.commit();
	}
	
	/**
	 * 删除学生
	 * @param event
	 */
	@FXML
	public void deleteStudent(ActionEvent event) {
		// TODO Autogenerated
        ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
//		要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
		Transaction transaction = studentDao.getSession().beginTransaction();
		
        Long id = Long.parseLong(stuDelId.getText());
		Student student = studentDao.findById(id);
		if(student != null) {
			msgBox.setText(student.getName() + student.getId());
			studentDao.delete(student);
			msgBox.setText(id + "学生删除成功：" + student.toString()+" \n");
		}else {
			msgBox.setText("学生不存在\n");
		}
		transaction.commit();
	}
	
	/**
	 * 更新学生信息
	 * @param event
	 */
	@FXML
	public void updateStudent(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
//		要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
		Transaction transaction = studentDao.getSession().beginTransaction();
		
        Long id = Long.parseLong(stuUpdId.getText());
        String newName = stuUpdName.getText();
		Student stu = studentDao.findById(id);
		if(stu != null) {
			stu.setName(newName);
			stu = studentDao.merge(stu);
			msgBox.setText("学生更新成功：" + stu.toString()+"\n");
		}else {
			msgBox.setText("学生不存在\n");
		}
		transaction.commit();
	}
	
	/**
	 * 更新教师信息
	 * @param event
	 */
	@FXML
	public void updateTeacher(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
//		要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
		Transaction transaction = teacherDao.getSession().beginTransaction();
		
        Long id = Long.parseLong(teaUpdId.getText());
        String newName = teaUpdName.getText();
		Teacher teacher = teacherDao.findById(id);
		if(teacher != null) {
			teacher.setName(newName);
			teacher = teacherDao.merge(teacher);
			msgBox.setText("教师信息更新成功：" + teacher.toString()+"\n");
		}else {
			msgBox.setText("教师不存在\n");
		}
		transaction.commit();
	}

	/**
	 * 删除教师
	 * @param event
	 */
	@FXML
	public void deleteTeacher(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
//		要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
		Transaction transaction = teacherDao.getSession().beginTransaction();
		
        Long id = Long.parseLong(teaDelId.getText());
		Teacher teacher = teacherDao.findById(id);
		if(teacher != null) {
			msgBox.setText(teacher.getName() + teacher.getId());
			teacherDao.delete(teacher);
			msgBox.setText(id + "教师删除成功：" + teacher.toString()+" \n");
		}else {
			msgBox.setText("学生不存在\n");
		}
		transaction.commit();
	}

	/**
	 * 增加教师
	 * @param event
	 */
	@FXML
	public void addTeacher(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
//		要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
		Transaction transaction = teacherDao.getSession().beginTransaction();
		Teacher teacher=new Teacher();
		teacher.setName(teaAddName.getText());
		teacher.setId(Long.parseLong(teaAddId.getText()));
		teacherDao.save(teacher);
		msgBox.setText(teaAddName.getText() + "教师添加成功:" + teacher.toString() + "\n");
		transaction.commit();
    }
	
	/**
	 * 学生选择导师
	 * @param event
	 */
	@FXML
	public void studentChooseTeacher(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		Transaction transaction = studentDao.getSession().beginTransaction();

		Long studentId = Long.parseLong(stuChooseSid.getText());
		Long teacherId = Long.parseLong(stuChooseTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}
		Teacher teacher = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}
		stu.setTeacher(teacher);
		studentDao.attachDirty(stu);
		msgBox.setText("学生 " + stu.toString()+" 绑定教师 "+teacher+" 成功\n");
		transaction.commit();
	}

	/**
	 * 学生更换绑定导师
	 * @param event
	 */
	@FXML
	public void studentUpdateTeacher(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		Transaction transaction = studentDao.getSession().beginTransaction();

		Long studentId = Long.parseLong(stuUpdSid.getText());
		Long teacherId = Long.parseLong(stuUpdTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}
		Teacher teacher = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}
		stu.setTeacher(teacher);
		studentDao.attachDirty(stu);
		msgBox.setText("学生 " + stu.toString()+" 绑定教师 "+teacher+" 成功\n");
		transaction.commit();
	}
	
	/**
	 * 教师取消绑定学生
	 */
	@FXML
	public void teacherCancelStudent(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		Transaction transaction = studentDao.getSession().beginTransaction();
		
		Long studentId = Long.parseLong(TeaUpdSid.getText());
		Long teacherId = Long.parseLong(TeaUpdTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}
		Teacher teacher = stu.getTeacher();
		Teacher teacher1 = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}else if(teacher.getId().longValue() != teacher1.getId().longValue()) {
			msgBox.setText("您不是 " + teacher.getId() +" 无法对学生 "+ stu.getId() +"操作\n");
			return;
		}
		stu.setTeacher(null);
		studentDao.attachDirty(stu);
		msgBox.setText("教师 " + teacher.getId() +" 取消绑定学生 "+stu.toString()+" 成功\n");
		transaction.commit();
	}
	
	/**
	 * 教师选择指导学生
	 */	@FXML
	public void teacherChooseStudent(ActionEvent event) {
		// TODO Autogenerated
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		Transaction transaction = studentDao.getSession().beginTransaction();
			
		Long studentId = Long.parseLong(TeaChooseSid.getText());
		Long teacherId = Long.parseLong(TeaChooseTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}else if(stu.getTeacher() != null) {
			msgBox.setText("学生已有指导教师\n");
			return;
		}
		Teacher teacher = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}
		stu.setTeacher(teacher);
		studentDao.attachDirty(stu);
		msgBox.setText("教师 " + teacher+" 绑定学生 "+stu.toString()+" 成功\n");
		transaction.commit();
	}
}
